<?php

/**
 * This is the model class for table "{{user}}".
 *
 * The followings are the available columns in table '{{user}}':
 * @property string $id
 * @property string $email
 * @property string $customer_name
 * @property string $mobile
 * @property string $address
 * @property string $sex
 * @property string $city
 * @property string $birthday
 * @property integer $status
 * @property string $referral_id
 * @property string $activate_code
 * @property integer $create_time
 * @property integer $update_time
 */
class User extends ActiveRecord
{
	const INACTIVE = 0;
	const ACTIVE = 1;
	
	public $create_time_from;
	public $create_time_to;
	public $update_time_from;
	public $update_time_to;

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{user}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('email, activate_code', 'required', 'message' =>'Vui lòng nhập {attribute}.'),
			array('email', 'unique'),
			array('status, create_time, update_time', 'numerical', 'integerOnly'=>true),
			array('email, activate_code', 'length', 'max'=>255),
			array('customer_name', 'length', 'max'=>50),
			array('mobile, referral_id', 'length', 'max'=>20),
			array('address', 'safe'),
			array('email', 'email', 'message'=>'Vui lòng nhập email hợp lệ.'),
			array('email', 'unique', 'message'=>'Email này đã đăng ký rồi.'),
			array('mobile', 'numerical', 'integerOnly'=>true, 'message'=>'Vui lòng nhập {attribute} hợp lệ.'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('city, birthday', 'safe'),
			array('id, email, customer_name, mobile, address, sex, status, referral_id, activate_code, create_time, update_time', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'referrer'=>array(self::BELONGS_TO, 'User', 'referral_id'),
			'referrals'=>array(self::HAS_MANY, 'User', 'referral_id'),
			'referralCount'=>array(self::STAT, 'User', 'referral_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'email' => 'Email',
			'customer_name' => 'Họ & Tên',
			'mobile' => 'ĐTDĐ',
			'address' => 'Địa chỉ',
			'status' => 'Status',
			'referral_id' => 'Referral ID',
			'activate_code' => 'Activate Code',
			'create_time' => 'Thời điểm đăng ký',
			'update_time' => 'Update Time',
			'sex' => 'Giới tính',
			'city' => 'Thành phố',
			'birthday' => 'Ngày sinh',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('customer_name',$this->customer_name,true);
		$criteria->compare('mobile',$this->mobile,true);
		$criteria->compare('address',$this->address,true);
		$criteria->compare('status',$this->status);
		$criteria->compare('referral_id',$this->referral_id,true);
		$criteria->compare('activate_code',$this->activate_code,true);
		$criteria->compare('create_time',$this->create_time);
		$criteria->compare('update_time',$this->update_time);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return User the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	public function generateActivateCode()
	{
		$this->activate_code = sha1(uniqid());
	}

	public static function getDySexList($index=NULL)
	{
		$listData = array('male'=>'Nam','female'=>'Nữ');
		if($index)
		{
			if(array_key_exists($index, $listData))
				return $listData[$index];
			else
				return NULL;
		}
		else
		{
			return $listData;
		}
	}	
}
